package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.Result;
import com.nit.server.entity.Community;
import com.nit.server.entity.Useapply;
import com.nit.server.entity.Userecord;
import com.nit.server.service.UseapplyService;
import com.nit.server.service.UserecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/userecord")
public class UserecordController {
    @Autowired
    private UserecordService userecordService;
    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Userecord userecord){
        return userecordService.save(userecord)?Result.success():Result.fail();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Userecord userecord){
        return userecordService.updateById(userecord)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        System.out.println(pageParam);
        Page<Userecord> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        String cId = (String)pageParam.getParams().get("cId");
        String userId = (String)pageParam.getParams().get("userId");
        QueryWrapper<Userecord> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.usegoods_id = b.usegoods_id and a.user_id = c.user_id ");
        if (StringUtils.isNotBlank(cId)){
            queryWrapper.eq("b.c_id",cId);
        }
        if (StringUtils.isNotBlank(userId)){
            queryWrapper.eq("a.user_id",userId);
        }
        IPage result = userecordService.pageCC(page,queryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
}
